/**
 * 自定义
 * */


//获取配置
function getConfig(){
    $.ajax({
        type: apis.config.method,
        url: apis.config.url,
        data: {},
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                console.log(res.msg);
                msgPop({msg: res.msg}); return
            }
            ewmDomain = res.data.domain;
        },
        error: function () {
            console.log('error');
        }
    });
}


//获取数据
function getUsedList(){
    type = 1;
    var titleDiv = $('#currTitle');
    titleDiv.html('当前是：使用列表');
    if (word) {
        pageEmptyStart = 0;
    }
    var formData = {
        pageStart: pageUseStart,
        pageLimit: pageUseLimit,
        word: word,
    };
    $.ajax({
        type: apis.used.method,
        url: apis.used.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                console.log(res.msg);
                msgPop({msg: res.msg}); return
            }
            dlist = res.data;
            usedHtml(res.data);
            titleDiv.html('当前是：使用列表，码' + res.total + '个');
        },
        error: function () {
            console.log('error');
        }
    });
}
//二维码已用
function usedHtml(d) {
    var h = '', i, ewmLink;
    //渲染主体
    for (i = 0; i < d.length; i ++) {
        ewmLink = d[i].ewmLink;
        h += '<a href="' + ewmLink + '" target="_blank">';
        if (i%4 == 0) {
            h += '<div class="li" style="margin-left: 0; height: 280px;">';
        } else {
            h += '<div class="li" style="height: 280px;">';
        }
        h += '<div class="ewm" title="' + ewmLink + '" id="qr' + i + '"></div>';
        h += '<div class="txt">';
        h += '<br/> 编号：' + d[i].show_code;
        h += '<br/> 访问量：' + d[i].visit_total;
        h += '</div>';
        h += '</div>';
        h += '</a>';
    }
    $('#body').html(h);
    pageType = 1;
    //渲染码
    for (i = 0; i < d.length; i ++) {
        ewmLink = d[i].ewmLink;
        $('#qr' + i).qrcode({
            render: 'canvas', text: ewmLink,
            width: 200, height: 200,
            background: "#fff", //背景颜色
            foreground: "#000", //前景颜色
            src: '/front/imgs/baim.png',
        });
    }
}


//空码请求
function getEmptyList(){
    type = 2;
    var titleDiv = $('#currTitle');
    titleDiv.html('当前是：空码列表');
    if (word) {
        pageEmptyStart = 0;
    }
    var formData = {
        pageStart: pageEmptyStart,
        pageLimit: pageEmptyLimit,
        word: word,
    };
    $.ajax({
        type: apis.empty.method,
        url: apis.empty.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                console.log(res.msg);
                msgPop({msg: res.msg}); return
            }
            dlist = res.data;
            emptyHtml(res.data);
            titleDiv.html('当前是：使用列表，码' + res.total + '个');
        },
        error: function () {
            console.log('error');
        }
    });
}
//二维码空码
function emptyHtml(d){
    var h = '', i, ewmLink;
    //渲染主体
    for (i = 0; i < d.length; i ++) {
        ewmLink = d[i].ewmLink;
        h += '<a href="' + ewmLink + '" target="_blank">';
        if (i%4 == 0) {
            h += '<div class="li" style="margin-left: 0; height: 250px;">';
        } else {
            h += '<div class="li" style="height: 250px;">';
        }
        h += '<div class="ewm" title="' + ewmLink + '" id="qr' + i + '"></div>';
        h += '<div class="txt" style="text-align: center;">';
        h += '编号：' + d[i].show_code;
        h += '</div>';
        h += '</div>';
        h += '</a>';
    }
    $('#body').html(h);
    pageType = 2;
    //渲染码
    for (i = 0; i < d.length; i ++) {
        ewmLink = d[i].ewmLink;
        $('#qr' + i).qrcode({
            render: 'canvas', text: ewmLink,
            width: 200, height: 200,
            background: "#fff", //背景颜色
            foreground: "#000", //前景颜色
            src: '/front/imgs/baim.png',
        });
    }
}


//分页
function reset(){
    window.location.href = '';
}


function next(){
    if (pageType == 1) {
        if (dlist.length < pageUseLimit) {
            msgPop({msg: '没有更多了'}); return
        }
        if (dlist.length == pageUseLimit) {
            pageUseStart ++;
        }
        getUsedList();
    } else {
        if (dlist.length < pageEmptyLimit) {
            msgPop({msg: '没有更多了'}); return
        }
        if (dlist.length == pageEmptyLimit) {
            pageEmptyStart ++;
        }
        getEmptyList();
    }
}


//码搜索
function getSearch(){
    word = $('#word').val();
    if (pageType == 1) {
        pageUseStart = 0;
        getUsedList();
    } else {
        pageEmptyStart = 0;
        getEmptyList();
    }
}